﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Text;
using Register.BLL;

namespace RegisterWeb.Manager.RegistrationFeeManager.ashx
{
    /// <summary>
    /// RegisteranalyseService 的摘要说明
    /// </summary>
    public class RegisteranalyseService : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.ContentType = "text/plain";
            string action = context.Request["action"];
            StringBuilder strBuilder = new StringBuilder();
            string Departments_ID = (string)context.Request["departments"];
            string OutpatientType = (string)context.Request["rfoutpatientype"];
            if (!String.IsNullOrEmpty(Departments_ID))
            {
                strBuilder.Append("   and    Departments_ID='").Append(Departments_ID).Append("' ");
            }
            if (!String.IsNullOrEmpty(OutpatientType))
            {
                strBuilder.Append(" and  Outpatient_OutpaitentType='").Append(OutpatientType).Append("' ");
            }
            if (action == "1")
            {
                String[] strdata = gettenlast(strBuilder.ToString());
                List<data> list = new List<data>();
                for (int i = 0; i < 10; i++)
                {
                    data dt = new data();
                    dt.ydata = strdata[i];
                    dt.xdata = DateTime.Now.AddDays(i - 10).ToString("yyyy-M-d");//"第" + (i + 1) + "天";
                    list.Add(dt);
                }
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string ss = jss.Serialize(list);
                context.Response.Write(ss);
            }
            else if (action == "2")
            {
                String[] strdata = GetLastMonth(strBuilder.ToString());
                List<data> list = new List<data>();
                for (int i = 0; i < strdata.Length; i++)
                {
                    data dt = new data();
                    dt.ydata = strdata[i];
                    dt.xdata = DateTime.Now.AddDays(i - strdata.Length ).ToString("yyyy-M-d");//"第" + (i + 1) + "天";
                    list.Add(dt);
                }
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string ss = jss.Serialize(list);
                context.Response.Write(ss);


            }
            else if (action == "3")
            {
                List<data> list = new List<data>();
                String starttime = DateTime.Now.Year.ToString() + "-1-1";
                String[] strdata = GetData(starttime, DateTime.Now.ToString("yyyy-M-d"), strBuilder.ToString());
                for (int i = 0; i < strdata.Length; i++)
                {
                    data dt = new data();
                    dt.ydata = strdata[i];
                    dt.xdata = DateTime.Now.ToString("yyyy") + "-" + (i + 1);
                    list.Add(dt);

                }
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string ss = jss.Serialize(list);
                context.Response.Write(ss);
            }
            else if (action == "4")
            {
                String sdata = context.Request["startData"];
                String edata = context.Request["endData"];
                List<data> list = new List<data>();
                String starttime = DateTime.Now.Year.ToString() + "-1-1";
                String[] strdata = GetData(sdata, edata, strBuilder.ToString());
                DateTime time = Convert.ToDateTime(sdata);
                //    int DateTime.Now.Month
                for (int i = 0; i < strdata.Length; i++)
                {
                    data dt = new data();
                    dt.ydata = strdata[i];
                    dt.xdata = time.AddMonths(i).ToString("yyyy-M");
                    list.Add(dt);
                }
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string ss = jss.Serialize(list);
                context.Response.Write(ss);
            }
            else if (action == "5")
            {
                String sdata = context.Request["startData"];
                String edata = context.Request["endData"];
                List<data> list = new List<data>();

                int sy = Convert.ToInt32(sdata.Substring(0, 4));
                int ey = Convert.ToInt32(edata.Substring(0, 4));
                String[] strdata = Getdatayear(sy, ey, strBuilder.ToString());
                for (int i = 0; i < strdata.Length; i++)
                {
                    data dt = new data();
                    dt.ydata = strdata[i];
                    dt.xdata = DateTime.Now.AddYears(i - strdata.Length + 1).ToString("yyyy");
                    list.Add(dt);
                }
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string ss = jss.Serialize(list);
                context.Response.Write(ss);
            }
        }
        public static String[] GetLastMonth(String where)
        {
            DateTime dt = DateTime.Now;
            int day = DateTime.Now.Day;
            TimeSpan ts = new TimeSpan(day, 0, 0, 0);
            DateTime result = dt.Subtract(ts);
            String[] dt1;
            dt1 = new String[day];
            for (int i = 0; i < day; i++)
            {
                DateTime sTime = result.AddDays(i);
                string dayTime = sTime.ToString("yyyy-MM-dd");
                string sql = "select count(*) from  Register_view  where   CONVERT( varchar(12) , Register_AddTime, 23 )='" + dayTime + "'  " + where;
                dt1[i] = RegisterviewanalyseBLL.GetCount(sql).ToString();
            }
            return dt1;
        }
        public static String[] gettenlast(String where)
        {
            DateTime dt = DateTime.Now;
            TimeSpan ts = new TimeSpan(10, 0, 0, 0);
            DateTime result = dt.Subtract(ts);
            String[] dt1;
            dt1 = new String[10];
            for (int i = 0; i < 10; i++)
            {
                DateTime sTime = result.AddDays(i);
                string dayTime = sTime.ToString("yyyy-MM-dd");
                string sql = "select count(*) from  Register_view  where   CONVERT( varchar(12) , Register_AddTime, 23 )='" + dayTime + "'  " + where;
                dt1[i] = RegisterviewanalyseBLL.GetCount(sql).ToString();
            }
            return dt1;
        }
        /// <summary>
        /// 自定义时间按年走势
        /// </summary>
        /// <param name="startYear">开始的年</param>
        /// <param name="endYear">结束的年</param>
        /// <param name="flag">按年标志</param>
        /// <param name="where">where语句</param>
        /// <returns></returns>
        public static String[] Getdatayear(int startYear, int endYear, string where)
        {
            String[] dt1 = new String[endYear - startYear + 1];
            for (int i = 0; i < endYear - startYear + 1; i++)
            {
                string startTime = (startYear + i).ToString() + "-1-1";
                string endTime = (startYear + i).ToString() + "-12-31";
                string sql = "select count(*) from  Register_view  where   Register_AddTime>='" + startTime + "' and  Register_AddTime<='" + endTime + "'  " + where;
                dt1[i] = RegisterviewanalyseBLL.GetCount(sql).ToString();
            }
            return dt1;
        }
        /// <summary>
        /// 自定义时间按月走势
        /// </summary>
        /// <param name="startTime">开始的时间</param>
        /// <param name="endTime">结束的时间</param>
        /// <param name="where">where语句</param>
        /// <returns></returns>
        public String[] GetData(string startTime, string endTime, string where)
        {
            DateTime sTime = DateTime.Parse(startTime);
            DateTime eTime = DateTime.Parse(endTime);
            if (sTime.Year == eTime.Year)
            {
                String[] dt1 = new String[eTime.Month - sTime.Month + 1];
                for (int i = 0; i <= eTime.Month - sTime.Month; i++)
                {
                    string time1 = sTime.Year.ToString() + "-" + (sTime.Month + i).ToString() + "-1";
                    string time2;
                    if (i + sTime.Month + 1 < 13)
                    {
                        time2 = sTime.Year.ToString() + "-" + (i + sTime.Month + 1).ToString() + "-1";
                    }
                    else
                    {
                        time2 = (sTime.Year + 1).ToString() + "-" + "1-1";
                    } string sql = "select count(*) from  Register_view where  Register_AddTime>='" + time1 + "' and Register_AddTime<='" + time2 + "'  " + where;

                    dt1[i] = RegisterviewanalyseBLL.GetCount(sql).ToString();
                }
                return dt1;
            }
            else
            {
                int j = 0; int a = 0;
                String[] dt1 = new String[12 - sTime.Month + 1 + (eTime.Year - sTime.Year - 1) * 12 + eTime.Month];
                for (int i = 0; i <= 12 - sTime.Month; i++, j++)
                {
                    a++;
                    string time1 = (sTime.Year).ToString() + "-" + (sTime.Month + i).ToString() + "-1";
                    string time2;
                    if (sTime.Month + i + 1 == 13)
                    {
                        time2 = (sTime.Year + 1).ToString() + "-" + (1).ToString() + "-1";
                    }
                    else
                        time2 = (sTime.Year).ToString() + "-" + (sTime.Month + i + 1).ToString() + "-1";
                    string sql = "select count(*) from  Register_view  where  Register_AddTime>='" + time1 + "' and Register_AddTime<='" + time2 + "'  " + where;
                    dt1[i] = RegisterviewanalyseBLL.GetCount(sql).ToString();
                }
                int z = 11; a = 0;
                int m = j;
                for (int i = j; i < m + (eTime.Year - sTime.Year - 1) * 12; i++, j++)
                {
                    a++; z++;
                    if (z % 12 == 0)
                    {
                        a = 1;
                    }
                    string time1 = (sTime.Year + z / 12).ToString() + "-" + a.ToString() + "-1";
                    string time2;
                    if (a == 12)
                    {
                        time2 = (sTime.Year + z / 12 + 1).ToString() + "-" + (1).ToString() + "-1";
                    }
                    else
                        time2 = (sTime.Year + z / 12).ToString() + "-" + (a + 1).ToString() + "-1";
                    string sql = "select count(*) from  Register_view where  Register_AddTime>='" + time1 + "' and Register_AddTime<='" + time2 + "'  " + where;
                    dt1[i] = RegisterviewanalyseBLL.GetCount(sql).ToString();
                }
                a = 1;
                m = j;
                for (int i = j; i < m + eTime.Month; i++, j++)
                {
                    string time1 = eTime.Year.ToString() + "-" + (a).ToString() + "-1";
                    string time2 = eTime.Year.ToString() + "-" + (a + 1).ToString() + "-1";
                    string sql = "select count(*) from  Register_view where  Register_AddTime>='" + time1 + "' and Register_AddTime<='" + time2 + "'  " + where;
                    dt1[i] = RegisterviewanalyseBLL.GetCount(sql).ToString();
                    a++;
                }
                return dt1;
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public class data
        {
            public String xdata { get; set; }
            public String ydata { get; set; }
        }

    
    }
}